package com.hnjowe.webdb.action;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.sys.service.SysService;

public class DatebackupAction extends ActionSupport{
	
	private static final long serialVersionUID = 4538771320689086966L;
	private SysService sysService = SysService.getInstance();
	private String successMessage;

	private String path="";
	private String path1="";
	private ByteArrayInputStream inputStream;
	
	private List<String> fileList;
	public DatebackupAction(){
		
	}
	
	public String backupaddload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			//String url = "\\"+File.separator+"192.168.0.6"+File.separator+"record"; 
			/*String url ="/upload/sentiment/";
			fileList = new ArrayList<String>();
			File f = new File(url); 
			String[] names = f.list();
	

			for(int i=0;i <names.length;i++) 
			{ 
				fileList.add(names[i]); 
			} */
			return "SUCCESS";
		}
	}
	
/*	File f = new File(url);
	if (!f.isDirectory()) {  
		
		  String filePath = f.getAbsolutePath();     
	   if (!suffix.equals("")) {  
		   int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引   
		   String tempsuffix = "";    
	   if (begIndex != -1) {  
		   tempsuffix = filePath.substring(begIndex + 1, filePath.length());  
		   if (tempsuffix.equals(suffix)) {  
			   fileList.add(filePath);  
	     }  
	    }  
	   } else {  
		   fileList.add(filePath);  
	   }  
	 }  
	System.out .println("aaaaaaaaaa"+fileList.size());
	*/
	public String startBackupDatavase(){  
	       //使用mysqldump来备份数据库mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql   
	    //String str="C:\\mysqldump -uroot -pzw@ZGHopen -hlocalhost union ";   
		//String str="/usr/local/dbserver/mysql/bin/mysqldump -usystem -pFkt3pDx6Vr -h 124.225.113.67 -P 3306 cocotour > d:/co.sql";
		//String str="C:\\mysqldump -usystem -pFkt3pDx6Vr -h 124.225.113.67 -P 3306 cocotour ";
		String str="C:\\mysqldump -uroot -pzw@zgh -h 113.59.34.56 -P 3306 union "; 
		//String str="C:\\mysqldump -uroot -pzw@ZGHopen -h localhost union "; 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		path = "union_"+sdf.format(new Date())+".sql";
		//path1="union_"+"20121122203001"+".sql";
        //String backFile = "> E:/union/WebRoot/upload/sentiment/"+path;//要备份的文件
		String backFile = "> D:/tomcat/webapps/myunion/datebackup/"+path;//要备份的文件
	       try {   
	               Runtime rt=Runtime.getRuntime();  
	               rt.exec("cmd /c"+str+backFile);
 
	            //上面可以cmd调用控制台，然后执行str中的字符串表示的命令。   
	               System.out.println("successly!");  
	       } catch (IOException e) {    
	        successMessage = "数据库备份失败!";  
	           e.printStackTrace();    
	           System.out.println("backup database wrong!");                  
	       }    
	       successMessage = "数据库备份成功!"; 
	       System.out.println(path); 
	       inputStream = new ByteArrayInputStream(path.toString().getBytes());
	    return SUCCESS;
	}  
	 
	
	 public String backup() {
		 String cmd="C:\\mysqldump -usystem -pFkt3pDx6Vr -h 124.225.113.67 -P 3306 cocotour";

		 
		    try 
		    {   
		     Runtime rt = Runtime.getRuntime();   
		        
		        // 调用 mysql 的 cmd:   
		        Process child = rt   
		        .exec("cmd /c "+cmd);// 设置导出编码为utf8。这里必须是utf8   
		  
		        // 把进程执行中的控制台输出信息写入.sql文件，即生成了备份文件。注：如果不对控制台信息进行读出，则会导致进程堵塞无法运行   
		        InputStream in = child.getInputStream();// 控制台的输出信息作为输入流   
		  
		        InputStreamReader xx = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8，否则从流中读入的是乱码   
		  
		        String inStr;   
		        StringBuffer sb = new StringBuffer("");   
		        String outStr;   
		        // 组合控制台输出信息字符串   
		        BufferedReader br = new BufferedReader(xx);   
		        while ((inStr = br.readLine()) != null) {   
		            sb.append(inStr + "\r\n");   
		        }   
		        outStr = sb.toString();   
		  
		        // 要用来做导入用的sql目标文件：   
		        FileOutputStream fout = new FileOutputStream("f:/aa.sql");   
		        OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");   
		        writer.write(outStr);   
		        // 注：这里如果用缓冲方式写入文件的话，会导致中文乱码，用flush()方法则可以避免   
		        writer.flush();   
		  
		        // 别忘记关闭输入输出流   
		        in.close();   
		        xx.close();   
		        br.close();   
		        writer.close();   
		        fout.close();   
		        System.out.println(" Output OK!");   

		  
		    } catch (Exception e) {   
		        e.printStackTrace();   
		        System.out.println("failure!<br>"  );
		    }   
		  
		    return "SUCCESS";   
		}   


	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getSuccessMessage() {
		return successMessage;
	}

	public void setSuccessMessage(String successMessage) {
		this.successMessage = successMessage;
	}

	public String getPath1() {
		return path1;
	}

	public void setPath1(String path1) {
		this.path1 = path1;
	}

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public List<String> getFileList() {
		return fileList;
	}

	public void setFileList(List<String> fileList) {
		this.fileList = fileList;
	}  

}